package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.BrandMapper;
import com.ybkj.daijia.server.mapper.BrandTypeMapper;
import com.ybkj.daijia.server.mapper.CarMapper;
import com.ybkj.daijia.server.mapper.CarTypeMapper;
import com.ybkj.daijia.server.mapper.DriverMapper;
import com.ybkj.daijia.server.model.Brand;
import com.ybkj.daijia.server.model.BrandType;
import com.ybkj.daijia.server.model.Car;
import com.ybkj.daijia.server.model.Car.TransmissionCase;
import com.ybkj.daijia.server.service.CarService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 */
@Service
public class DefaultCarService implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private BrandTypeMapper brandTypeMapper;

    @Autowired
    private CarTypeMapper carTypeMapper;

    @Autowired
    private SettingUtils settingUtils;

//	@Autowired
//	private Rent


    //分页查询
    public Page<Car> queryAll(String keyWords) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        List<Car> content = carMapper
            .selectWithParams(keyWords, Pager.getOffset(), Pager.getLimit());
        int total = carMapper.countWithParams(keyWords);

        return new PageImpl<Car>(content, pageable, total);
    }

    //添加一条完整记录
    @Transactional(rollbackFor = Exception.class)
    public Car insertRecord(Car car) {

        carMapper.insertRecord(car);

        return car;
    }

    //根据车牌号号查询一组信息
    public Car findByNumber(String number) {

        return carMapper.selectWithNumber(number);
    }

    //查询关联集合
    public Page<Car> findCarResult(String param) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        String likeParam = null;
        if (StringUtils.isNotBlank(param)) {
            likeParam = "%" + param + "%";
        }

        List<Car> content = carMapper
            .selectListWithParams(likeParam, Pager.getOffset(), Pager.getLimit());
        int total = carMapper.countWithParams(likeParam);

        return new PageImpl<Car>(content, pageable, total);

    }

    public List<Car> findByNumberLike(String number) {

        return carMapper.selectWithNumberLike("%" + number + "%");

    }


    public List<Car> findALL() {
        List<Car> list = carMapper.selectAll();
        return list;

    }


    @Override
    public Car findById(Long id) {
        return carMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(Car record) {

        return carMapper.updateByPrimaryKey(record);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteCarType(Long carTypeId) {
        return carMapper.deleteCarTypeId(carTypeId);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteBrand(Long brandId) {
        return carMapper.deleteBrandId(brandId);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteBrandType(Long brandTypeId) {
        return carMapper.deleteBrandTypeId(brandTypeId);
    }

    @Transactional(rollbackFor = Exception.class)
    public int delete(Car car) {

        //删除司机关联
        driverMapper.deleteCarRelation(car.getId());

        return carMapper.deleteByPrimaryKey(car.getId());
    }

    @Override
    public Page<Map<String, Object>> findAllWithParam(String city, BigDecimal max, BigDecimal min,
        Long brandId, Long[] array,
        String sequenceStr, TransmissionCase transmissionStr, Integer page, Integer limit) {

        Pageable pageable = new PageRequest(page, limit);

        List<Car> list = carMapper
            .selectWithParam(city, max, min, brandId, array, sequenceStr, transmissionStr,
                page * limit, limit);

        List<Map<String, Object>> carList = new ArrayList<Map<String, Object>>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (Car c : list) {

                Map<String, Object> map = new HashMap<String, Object>();
                map.put("brandId", c.getBrandId());
                Brand brand = brandMapper.selectByBrandId(c.getBrandId());
                BrandType brandType = brandTypeMapper.selectById(c.getBrandTypeId());
                if (null != brand && null != brandType) {
                    map.put("brandName", brand.getName() + " - " + brandType.getName());
                } else {
                    map.put("brandName", "");
                }
                map.put("price", c.getPrice());
                map.put("displacement", c.getDisplacement());
                map.put("seating", c.getSeating());
                if (StringUtils.isNotBlank(c.getPicture())) {
                    map.put("picture", c.getPicture());
                } else {
                    map.put("picture", "");
                }
                map.put("limitLine", c.getLimitLine());
                map.put("carNumber", c.getCarNumber());
                map.put("type", c.getType());
                map.put("carId", c.getId());
                map.put("carTypeId", c.getCarTypeId());
                map.put("carType", carTypeMapper.selectById(c.getCarTypeId()).getName());
                String transmissionCase = "手动挡";
                if (TransmissionCase.AT.equals(c.getTransmissionCase())) {
                    transmissionCase = "自动挡";
                }
                map.put("transmissionCase", transmissionCase);
                carList.add(map);

            }

        }
        long total = carMapper.countWithParam(city, max, min, brandId, array, transmissionStr);

        Page<Map<String, Object>> result = new PageImpl<Map<String, Object>>(carList, pageable,
            total);

        return result;
    }

    @Override
    public List<Car> findByParams(Long companyId, Long brandId, Long brandTypeId) {
        List<Car> list = carMapper.selectByParams(companyId, brandId, brandTypeId);
        return list;
    }

    @Override
    public Long countCarByTypeId(Long typeId) {
        return carMapper.countCarByTypeId(typeId);
    }


}
